package 线程池.unsafe实现自旋锁;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author caihe
 * @date 2021/10/20 15:28
 */
public class UnsafeLockTest {
    
    static int i = 0;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        UnsafeLock unsafeLock = new UnsafeLock();
        // 设置等待两个线程任务完成
        CountDownLatch countDownLatch = new CountDownLatch(2);
        for (int j = 0; j < 2; j++) {
            executorService.execute(()->{
                unsafeLock.lock();
                for (int k = 0; k < 10000; k++) {
                    i++;
                }
                unsafeLock.unLock();
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println(i);
        executorService.shutdownNow();
    }
    
}
